require 'Common/define'
require 'Logic/Config'
require 'Common/functions'

UIOpenPopup = {}
HelperFunc.SetUIEnv(UIOpenPopup)
local this = UIOpenPopup

function UIOpenPopup.Init(data, closeFunc) 
    this.state = data
    local _name, _package = getUIName(PanelNames.UIOpenPopup)
    panelMgr:ShowUI(_name, _package, this.onCreate, Config.useLocalUI)
end

function UIOpenPopup.onCreate(obj) 
    if not this.hasInit then
        this.hasInit = true  
        this.btn_bg_close = obj:GetChild("BgClose")   
        this.btn_cancel = obj:GetChild("btnCancel")
        this.btn_sure = obj:GetChild("btnSure")
        this.txt_controller = obj:GetController("Tip")
        this.txt_open = obj:GetChild("Text_Open")
        this.txt_close = obj:GetChild("Text_Close")
        this.txt_open.text = getLanguage("CommonPop_Tips65")
        this.txt_close.text = getLanguage("CommonPop_Tips66")

        this.addEvent()
    end 
    this.initData()
end
 
function UIOpenPopup.initData() 
    if this.state and this.state == 1 then
        this.txt_controller:SetSelectedIndex(1)
    else
        this.txt_controller:SetSelectedIndex(0)
    end
end
 


function UIOpenPopup.addEvent() 
    this.btn_bg_close.onClick:Add(this.CloseSelf)    
    this.btn_cancel.onClick:Add(this.CloseSelf)    
    this.btn_sure.onClick:Add(this.onClickSure)    
end

function UIOpenPopup.removeEvent() 
    this.btn_bg_close.onClick:Clear()    
end

function UIOpenPopup.CloseSelf() 
    DestoryUI(PanelNames.UIOpenPopup)
end

function UIOpenPopup.onClickSure() 
    if this.state and this.state == 1 then
        local remain_time = dataTable.getDataTableByStringId("t_global", 363 ,"f_int_value")
        local dis_time = plunderHandler.last_change_func_time + remain_time  - Config.serverTime 
        if dis_time > 0 then 
            UISysTips.AddMsg(getLanguage("ErrorCode_172083"))
        else
            plunderHandler.sendReqChangePlunderFunction( 0 )
        end
    else
        plunderHandler.sendReqChangePlunderFunction( 1 )
    end
    DestoryUI(PanelNames.UIOpenPopup)
    FireEvent(Config.EventType.Refresh_OpenPlunder,true)
end
     
function UIOpenPopup.OnDestroy() 
    this.removeEvent()
    this.hasInit = nil  
end

return UIOpenPopup
